package com.lijiajian.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijiajian.base.BaseService;
import com.lijiajian.dao.SaleChanceMapper;
import com.lijiajian.enums.DevResult;
import com.lijiajian.enums.StateStatus;
import com.lijiajian.pojo.SaleChance;
import com.lijiajian.query.SaleChanceQuery;
import com.lijiajian.utils.AssertUtil;
import com.lijiajian.utils.PhoneUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SaleChanceService extends BaseService<SaleChance,Integer> {
    @Resource
    private SaleChanceMapper saleChanceMapper;

    /**
     * 多条件分页查询营销机会
     * @param saleChanceQuery
     * @return
     */
    public Map<String,Object> querySaleChanceByParams(SaleChanceQuery saleChanceQuery){
        Map<String,Object> map=new HashMap<>();
        //开启分页
        PageHelper.startPage(saleChanceQuery.getPage(),saleChanceQuery.getLimit());
        PageInfo<SaleChance> pageInfo=new PageInfo<>(saleChanceMapper.selectByParams(saleChanceQuery));
        map.put("code",0);
        map.put("msg","success");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());

        return map;
    }

    /**
     * 添加营销机会
     * @param saleChance
     */
    @Transactional(propagation= Propagation.REQUIRED)
     public void addSaleChance(SaleChance saleChance){
        //参数校验
        checkSaleChanceParams(saleChance.getCustomerName(),saleChance.getLinkMan(),saleChance.getLinkPhone());
        //设置相关字段的默认值
        saleChance.setIsValid(1);
        saleChance.setCreateDate(new Date());
        saleChance.setUpdateDate(new Date());
        //判断是否设置了指派人
        if(StringUtils.isBlank(saleChance.getAssignMan())){
            //为空，表示未设置指派人
            //为state分配状态,0未分配，1已分配
            saleChance.setState(StateStatus.UNSTATE.getType());
            //assignTime指派时间,设置为null
            saleChance.setAssignTime(null);
            //devResult开发状态,0未开发，1开发中，2开发成功，3开发失败
            saleChance.setDevResult(DevResult.UNDEV.getStatus());


        }else{
            //如果不为空,表示设置了指派人
            //state分配状态
            saleChance.setState(StateStatus.STATED.getType());
            saleChance.setAssignTime(new Date());
            saleChance.setDevResult(DevResult.DEVING.getStatus());


        }
        //执行添加操作，判断受影响的行数
        AssertUtil.isTrue(saleChanceMapper.insertSelective(saleChance)<1,"添加营销机会失败");
    }

    /**
     * 更新营销机会
     * @param saleChance
     */
    @Transactional(propagation=Propagation.REQUIRED)
    public void updateSaleChance(SaleChance saleChance){
        //参数校验（id、customerName,linkMan,linkPhone）
        AssertUtil.isTrue(null==saleChance.getId(),"待更新记录不存在");
        SaleChance temp = saleChanceMapper.selectByPrimaryKey(saleChance.getId());

        AssertUtil.isTrue(null==temp,"待更新记录不存在");
        checkSaleChanceParams(saleChance.getCustomerName(),saleChance.getLinkMan(),saleChance.getLinkPhone());
        // 设置相关参数的默认值：
        // updateTime:更新为当前系统时间
        saleChance.setUpdateDate(new Date());
        /*assignMan指派人：
        未设置：
        修改后未设置：
        不需要操作
        修改后已设置：
        assignTime（当前系统时间）、state=1、devResult=1

        已设置：
        修改后未设置：
        assignTime=null、state=0、devResult=0
        修改后已设置：
        判断是否是同一个人
        是，把assginTime设置成原来的时间
        不是，更新assignTime*/
        if(StringUtils.isBlank(temp.getAssignMan())){
            if(!StringUtils.isBlank(saleChance.getAssignMan())){
                saleChance.setAssignTime(new Date());
                saleChance.setState(StateStatus.STATED.getType());
                saleChance.setDevResult(DevResult.DEVING.getStatus());

            }
        }else{
            if(StringUtils.isBlank(saleChance.getAssignMan())){
                saleChance.setAssignTime(null);
                saleChance.setState(StateStatus.UNSTATE.getType());
                saleChance.setDevResult(DevResult.UNDEV.getStatus());
            }
            else{
                if(!saleChance.getAssignMan().equals(temp.getAssignMan())){
                    saleChance.setAssignTime(new Date());
                }else{
                    saleChance.setAssignTime(temp.getAssignTime());
                }
            }

        }
        //执行更新操作，判断受影响的行数
        AssertUtil.isTrue(saleChanceMapper.updateByPrimaryKeySelective(saleChance)<1,"更新失败");


    }

    /**
     * 更新营销机会状态
     * @param id
     * @param devResult
     */
    @Transactional(propagation=Propagation.REQUIRED)
    public  void updateSaleChanceDevResult(Integer id, Integer devResult){
        AssertUtil.isTrue(null==id,"数据异常");
        SaleChance saleChance = saleChanceMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(null==saleChance,"记录不存在");
        saleChance.setDevResult(devResult);

        AssertUtil.isTrue(saleChanceMapper.updateByPrimaryKeySelective(saleChance)<1,"更新失败");

    }
    @Transactional(propagation=Propagation.REQUIRED)
    public void deleteSaleChances(Integer[] ids){
        AssertUtil.isTrue(null==ids||ids.length==0,"请选择要删除的记录");
        AssertUtil.isTrue(saleChanceMapper.deleteBatch(ids)!=ids.length,"删除记录失败");
    }

    private void checkSaleChanceParams(String customerName, String linkMan, String linkPhone) {
        AssertUtil.isTrue(StringUtils.isBlank(customerName),"客户名不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(linkMan),"联系人不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(linkPhone),"联系电话不能为空");
        //电话是否符合格式
        AssertUtil.isTrue(!PhoneUtil.isMobile(linkPhone),"联系电话格式不正确");
    }



}
